package nl.weeaboo.sh;

import java.io.File;
import java.io.IOException;

import org.xml.sax.SAXException;

import timon.common.xml.XmlElement;
import timon.common.xml.XmlReader;

import static timon.common.xml.XmlUtil.*;

public class Config {

	private float   soundVolume          = 1.0f;
	private boolean vsyncEnabled         = false;
	private boolean useTrueFullscreen    = true;
	private boolean screenCaptureEnabled = false;
	private int     screenCaptureCRF     = 30;
	private boolean showComboInfo        = false;
	private GamepadConfig gamepadConfig  = new GamepadConfig(0, 0, 1, 2, 3);
	
	protected Config() {
	}
	
	//Functions
	public void load(String filename) throws IOException {
		XmlReader reader = new XmlReader();
		try {
			XmlElement root = reader.read(new File(filename));
			root = root.getChild("config");
			if (!checkXmlVersion(root.getAttribute("version"))) {
				throw new IOException("Invalid version: " + root.getAttribute("version"));
			}
			
			soundVolume = getFloat(root, "soundVolume", soundVolume);
			vsyncEnabled = getBoolean(root, "vsyncEnabled", vsyncEnabled);
			useTrueFullscreen = getBoolean(root, "useTrueFullscreen", useTrueFullscreen);
			screenCaptureEnabled = getBoolean(root, "screenCaptureEnabled", screenCaptureEnabled);
			screenCaptureCRF = getInt(root, "screenCaptureCRF", screenCaptureCRF);
			showComboInfo = getBoolean(root, "showComboInfo", showComboInfo);
			
			XmlElement gamepadE = root.getChild("gamepad");
			if (gamepadE != null) {
				gamepadConfig = new GamepadConfig(
						getIntAttribute(gamepadE, "id", gamepadConfig.getId()),
						getInt(gamepadE, "attack", gamepadConfig.getKey(0)),
						getInt(gamepadE, "jump", gamepadConfig.getKey(1)),
						getInt(gamepadE, "magic", gamepadConfig.getKey(2)),
						getInt(gamepadE, "pause", gamepadConfig.getKey(3)));
			}
		} catch (SAXException e) {
			throw new IOException(e);
		}
	}

	protected boolean checkXmlVersion(String version) {
		return version.compareTo("1.0.0") == 0;
	}

	//Getters
	public float   getSoundVolume() { return soundVolume; }
	public boolean isScreenCaptureEnabled() { return screenCaptureEnabled; }
	public int     getScreenCaptureCRF() { return screenCaptureCRF; }
	public boolean isVSyncEnabled() { return vsyncEnabled; }
	public boolean getUseTrueFullscreen() { return useTrueFullscreen; }
	public boolean getShowComboInfo() { return showComboInfo; }
	public GamepadConfig getGamepadConfig() { return gamepadConfig; }
	
	//Setters
	public void setSoundVolume(float v) { soundVolume = v; }
	
	//Inner Classes
	public static class GamepadConfig {
	
		private int id;
		private int keys[] = new int[4];
		
		private GamepadConfig(int id, int key0, int key1, int key2, int key3) {
			this.id = id;
			this.keys[0] = key0;
			this.keys[1] = key1;
			this.keys[2] = key2;
			this.keys[3] = key3;
		}
		
		public int getId() { return id; }
		public int getKey(int index) { return (index >= 0 && index < keys.length ? keys[index] : -1); }
	}
}
